package com.androidx.p013lv.base.http.callback;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import androidx.core.app.NotificationCompat;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.BaseSdk;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.dialog.DialogLoading;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.utils.AESEncryptUtils;
import com.androidx.p013lv.base.utils.BuildConfigUtils;
import com.androidx.p013lv.base.utils.DeviceIDUtils;
import com.androidx.p013lv.base.utils.FileListUtils;
import com.androidx.p013lv.base.utils.JsonUtils;
import com.androidx.p013lv.base.utils.LogUtils;
import com.androidx.p013lv.base.utils.SpUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.yalantis.ucrop.view.CropImageView;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Objects;

/* renamed from: com.androidx.lv.base.http.callback.HttpCallbackM3u8 */
public abstract class HttpCallbackM3u8<T> extends AbsCallback<T> implements HttpLvCallback<T> {
    Context context;
    Dialog dialog;
    public Gson gson = new Gson();
    protected String tag;
    public String url;

    public HttpCallbackM3u8(String str) {
        this.tag = str;
        this.context = null;
    }

    public HttpCallbackM3u8(String str, Context context2) {
        this.tag = str;
        this.context = context2;
        this.dialog = new DialogLoading(this.context);
    }

    public String getTag() {
        return this.tag;
    }

    @Override // com.lzy.okgo.callback.AbsCallback, com.lzy.okgo.callback.Callback
    public void onStart(Request<T, ? extends Request> request) {
        Dialog dialog2;
        if (!(this.context == null || (dialog2 = this.dialog) == null)) {
            dialog2.show();
        }
        this.url = request.getUrl();
        request.headers(HttpHeaders.HEAD_KEY_CONTENT_TYPE, "application/json");
        request.headers(HttpHeaders.HEAD_KEY_ACCEPT, "application/json");
        request.headers("deviceId", DeviceIDUtils.getUniqueId(BaseSdk.getContext()));
        String valueOf = String.valueOf(System.currentTimeMillis());
        String substring = valueOf.substring(3, 8);
        request.headers("t", valueOf);
        try {
            String md5 = FileListUtils.getMD5(substring);
            request.headers("s", md5 + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String string = SpUtils.getInstance().getString(Key.TOKEN);
        if (!TextUtils.isEmpty(string)) {
            request.headers("aut", string);
        }
        if (!TextUtils.isEmpty(BaseSdk.UserAgent)) {
            request.headers(HttpHeaders.HEAD_KEY_USER_AGENT, BaseSdk.UserAgent);
        }
        super.onStart(request);
    }

    @Override // com.lzy.okgo.callback.AbsCallback, com.lzy.okgo.callback.Callback
    public void onFinish() {
        try {
            if (!(this.context == null || this.dialog == null || !this.dialog.isShowing())) {
                this.dialog.dismiss();
            }
        } catch (Exception unused) {
            this.dialog = null;
        }
        super.onFinish();
    }

    @Override // com.lzy.okgo.callback.Callback
    public void onSuccess(Response<T> response) {
        onLvSuccess(response.body());
    }

    /* JADX DEBUG: Multi-variable search result rejected for r2v0, resolved type: com.androidx.lv.base.http.callback.HttpCallbackM3u8<T> */
    /* JADX WARN: Multi-variable type inference failed */
    @Override // com.lzy.okgo.callback.AbsCallback, com.lzy.okgo.callback.Callback
    public void onError(Response<T> response) {
        BaseRes baseRes;
        super.onError(response);
        try {
            String message = response.getException().getMessage();
            baseRes = new BaseRes(JsonUtils.getInt("code", message), JsonUtils.getString(NotificationCompat.CATEGORY_MESSAGE, message));
        } catch (Exception unused) {
            baseRes = new BaseRes(CropImageView.DEFAULT_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION, "未知錯誤，請稍後再試");
        }
        onLvSuccess(baseRes);
    }

    /* JADX DEBUG: Multi-variable search result rejected for r2v12, resolved type: T */
    /* JADX WARN: Multi-variable type inference failed */
    @Override // com.lzy.okgo.convert.Converter
    public T convertResponse(okhttp3.Response response) throws Throwable {
        Type type = ((ParameterizedType) Objects.requireNonNull(getClass().getGenericSuperclass())).getActualTypeArguments()[0];
        if (BuildConfigUtils.isIsDebug()) {
            LogUtils.m63e("okgo--", this.url);
        }
        if (response.code() == 200) {
            String string = response.body().string();
            if (BuildConfigUtils.isIsDebug()) {
                LogUtils.m63e("okgo--", "日志:" + string);
            }
            T t = (T) ((BaseRes) this.gson.fromJson(string, type));
            String encData = t.getEncData();
            if (!TextUtils.isEmpty(encData)) {
                String decrypt = AESEncryptUtils.decrypt(encData);
                if (BuildConfigUtils.isIsDebug()) {
                    LogUtils.m63e("okgo--", "解密后:" + decrypt);
                }
                t.setData(this.gson.fromJson(decrypt, ((ParameterizedType) type).getActualTypeArguments()[0]));
            }
            response.close();
            return t;
        }
        BaseRes baseRes = (BaseRes) this.gson.fromJson(response.body().string(), type);
        if (response.code() == 301) {
            String header = response.header("refresh-authorization", "");
            if (!TextUtils.isEmpty(header)) {
                SpUtils.getInstance().put(Key.TOKEN, header);
            }
            baseRes.setMsg("請重試");
        } else if (response.code() == 302) {
            SpUtils.getInstance().setUserInfo(null);
            ARouter.getInstance().build(Constance.LOGIN_ACTIVITY).withInt("type", 1).withInt(Key.STATION_TYPE, 1).navigation();
            baseRes.setCode(302);
            baseRes.setMsg("請重新登錄");
        } else if (response.code() != 1001) {
            baseRes.getCode();
        }
        String json = new Gson().toJson(baseRes);
        response.close();
        throw new IllegalStateException(json);
    }
}
